package com.superbytecode.cloud.common.conts;

import com.superbytecode.cloud.common.exception.IllegalArgsException;
import lombok.experimental.UtilityClass;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.common.conts
 * @ClassName: Duration
 * @Description: [常用时间段]
 * @Author: [Lance Ting]
 * @Date: 2024/11/13 12:22
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@UtilityClass
public class Duration {

    public final String UNIT_SECOND = "s";
    public final String UNIT_MINUTE = "m";
    public final String UNIT_HOUR = "h";
    public final String UNIT_DAY = "d";
    public final String UNIT_MONTH = "m";
    public final String UNIT_YEAR = "y";

    // 过期时间（秒）
    public final int CACHE_SECOND = 1;
    public final int CACHE_MINUTE = 60;
    public final int CACHE_HOUR = 60 * CACHE_MINUTE;
    public final int CACHE_DAY = 24 * CACHE_HOUR;
    public final int CACHE_MONTH = 30 * CACHE_DAY;
    public final int CACHE_YEAR = 12 * CACHE_MONTH;
    /**
     * 永不过期
     */
    public final int CACHE_FOREVER = 0;

    public long ofSecond(long time, String timeUnit) {
        long limitTimeOfSecond;
        switch (timeUnit) {
            case UNIT_SECOND:
                limitTimeOfSecond = time;
                break;
            case UNIT_MINUTE:
                limitTimeOfSecond = time * CACHE_MINUTE;
                break;
            case UNIT_HOUR:
                limitTimeOfSecond = time * CACHE_HOUR;
                break;
            case UNIT_DAY:
                limitTimeOfSecond = time * CACHE_DAY;
                break;
            default:
                throw new IllegalArgsException("not support type [%s] for time unit !", timeUnit);
        }
        return limitTimeOfSecond;
    }
}